#导入cv模块
import cv2 as cv
#检测函数
def face_detect_demo(img):
    gary = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    face_detect = cv.CascadeClassifier('./static/haarcascade/haarcascade_frontalface_default.xml')
    # [ [x, y, w, h] ]
    face = face_detect.detectMultiScale(gary, 1.1, 5)
    for x,y,w,h in face:
        cv.rectangle(img,(x,y),(x+w,y+h),color=(0,0,255),thickness=2)
        # 临时添加标签
        cv.putText(img, 'person', (x, y - 10), cv.FONT_HERSHEY_SIMPLEX, 1.2, (255, 0, 0), 3)
    return img


#读取摄像头
cap = cv.VideoCapture(0)
#循环
while True:
    flag,frame = cap.read()
    if not flag:
        break
    img = face_detect_demo(frame)
    cv.imshow('result', img)
    if ord('q') == cv.waitKey(1):
        break
#释放内存
cv.destroyAllWindows()
#释放摄像头
cap.release()